/*
 * @Author: 四川华西绿舍建材有限公司
 * @Date: 2023-10-22 10:59:51
 * @LastEditors: 陈波
 * @LastEditTime: 2024-04-26 17:12:28
 * @Description: 
 * @FilePath: \graDesign\src\router\index.ts
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// @ts-ignore
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// Layout Component
import Layout from '@/layout/index.vue'
// 消息弹出框
import { showMessageBox, showNotification } from "@/utils/alertMsg";
// 加载
import { showLoading, hideLoading } from '@/request/http'
import useMenuDataStore from "@/store/menuDataSource";
import useLoginStore from "@/store/login";
import { storeToRefs } from "pinia";

const routes: Array<RouteRecordRaw> = [
  /* 用户模块 User_Module 开始 */
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: async () => await import('@/views/User/Login/index.vue')
    // beforeEnter: (to, form, next) => { 函数逻辑 }
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      title: '注册'
    },
    component: async () => await import('@/views/User/Register/index.vue')
  },
  {
    path: '/retrievePassword',
    name: 'RetrievePassword',
    meta: {
      title: '找回密码'
    },
    component: async () => await import('@/views/User/RetrievePassword/index.vue')
  },
  /* 用户模块 User_Module 结束 */

  /* 布局模块 主要模块 Layout_Module 开始 */
  {
    path: '/',
    name: 'main',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: '首页'
        },
        component: () => import('@/views/Home/index.vue')
      },
      {
        path: '/proIntro',
        name: 'ProIntro',
        meta: {
          title: '项目简介'
        },
        component: () => import('@/views/ProIntro/index.vue')
      },
      {
        path: '/TD3AlgorithmCompare',
        name: 'TD3AlgorithmCompare',
        meta: {
          title: 'TD3算法对比'
        },
        component: () => import('@/views/TD3/TD3AlgorithmCompare/index.vue')
      },
      {
        path: '/TD3AlgorithmVisual',
        name: 'TD3AlgorithmVisual',
        meta: {
          title: 'TD3算法可视化'
        },
        component: () => import('@/views/TD3/TD3AlgorithmVisual/index.vue')
      },
      {
        path: '/SACAlgorithmCompare',
        name: 'SACAlgorithmCompare',
        meta: {
          title: 'SAC算法对比'
        },
        component: () => import('@/views/SAC/SACAlgorithmCompare/index.vue')
      },
      {
        path: '/SACAlgorithmVisual',
        name: 'SACAlgorithmVisual',
        meta: {
          title: 'SAC算法可视化'
        },
        component: () => import('@/views/SAC/SACAlgorithmVisual/index.vue')
      },
      {
        path: '/userManagement',
        name:'systemManagement-userManagement',
        meta: {
          title: '用户管理'
        },
        component: () => import('@/views/SystemManagement/UserManagement/index.vue')
      },
      {
        path: '/roleManagement',
        name:'systemManagement-roleManagement',
        meta: {
          title: '角色管理'
        },
        component: () => import('@/views/SystemManagement/RoleManagement/index.vue')
      },
      {
        path: '/personalInfo',
        name:'PersonalInfo',
        meta: {
          title: '个人中心'
        },
        component: () => import('@/views/UserInfo/index.vue')
      },
      {
        path: '/dataMock',
        name: 'DataMock',
        meta: {
          title: '数据模拟器'
        },
        component: () => import('@/views/DataMock/index.vue')
      },
      {
        path: '/tentative',
        name: 'Tentative',
        meta: {
          title: '暂定'
        },
        component: () => import('@/views/Tentative/index.vue')
      },
      {
        path: '/dataBigScreenIndex',
        name: 'DataBigScreenIndex',
        meta: {
          title: '数据大屏'
        },
        component: () => import('@/views/FirstNavIndex/dataBigScreenIndex.vue')
      },
      {
        path: '/settingIndex',
        name: 'SettingIndex',
        meta: {
          title: '系统设置'
        },
        component: () => import('@/views/FirstNavIndex/settingIndex.vue')
      },
    ]
  },
  /* 布局模块 主要模块 Layout_Module 结束 */

  /* 数据大屏 开始 */
  // {
  //   path: '/dataBigScreen',
  //   name: 'dataBigScreen',
  //   meta: {
  //     title: '数据大屏'
  //   },
  //   component: async () => await import('@/views/DataBigScreen/index.vue')
  // },

  {
    path: '/TD3DataBigScreen3D',
    name: 'TD3DataBigScreen3D',
    meta: {
      title: 'TD3-数据大屏(3D)'
    },
    component: async () => await import('@/views/DataBigScreen/TD3/indexThree.vue')
  },
  {
    path: '/TD3DataBigScreen2D',
    name: 'TD3DataBigScreen2D',
    meta: {
      title: 'TD3-数据大屏(2D)'
    },
    component: async () => await import('@/views/DataBigScreen/TD3/indexTwo.vue')
  },
  {
    path: '/SACDataBigScreen3D',
    name: 'SACDataBigScreen3D',
    meta: {
      title: 'SAC-数据大屏(3D)'
    },
    component: async () => await import('@/views/DataBigScreen/SAC/indexThree.vue')
  },
  {
    path: '/SACDataBigScreen2D',
    name: 'SACDataBigScreen2D',
    meta: {
      title: 'SAC-数据大屏(2D)'
    },
    component: async () => await import('@/views/DataBigScreen/SAC/indexTwo.vue')
  },
  /* 数据大屏 结束 */

  /* 404 页面 开始 */
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: '404',
  //   meta: {
  //     title: '404 Page'
  //   },
  //   component: async () => await import('@/views/Error/404.vue')
  // },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    meta: {
      title: '404 Page'
    },
    component: async () => await import('@/views/Error/errorIndex.vue')
  }
  /* 404 页面 结束 */
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局路由导航守卫
router.beforeEach((to, from, next) => {
  // @ts-ignore
  document.title = to.meta.title ? to.meta.title : 'vite + vue3 + TS'
  // 页面渲染成功之后，展示进度条（实际效果：Mac的Chrome就是在页面顶部有条2px左右的进度条）
  NProgress.start()
  showLoading();
  
  // 获取token do something
  const token = localStorage.getItem("access-token")
  // 不需要token也能放行的页面
  const noNeedTokenPages = ['login', 'register', 'RetrievePassword', '404']
  // 需要权限的页面
  const haveAuthPages = ['systemManagement-userManagement', 'systemManagement-roleManagement', 'Tentative', 'DataMock'];

  const menuDataStore = useMenuDataStore();
  const loginStore = useLoginStore();
  // const { getAuthHrefs: haveAuthPages } = storeToRefs(menuDataStore);
  const { userInfo } = storeToRefs(loginStore);

  // 判断有没有登录
  if(!noNeedTokenPages.includes(to.name as string) && !token) {
    showNotification('error', '错误', '未能获取到TOKEN')
    next('/404')
    hideLoading()
  } 
  else if (haveAuthPages.includes(to.name as string) && userInfo.value.ismanager == 0) {
    showMessageBox('error', '没有权限访问')
    showNotification('error', '错误', '没有权限')
    next('/404')
    hideLoading()
  } 
  else {
    next()
  }
})

// 页面加载成功之后，关闭进度条
router.afterEach(to => {
  hideLoading()
  NProgress.done()
})

export default router
